package cn.edu.scau.nursingcenter.controller;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.scau.nursingcenter.bean.ResultBean;
import cn.edu.scau.nursingcenter.entiry.Check_in;
import cn.edu.scau.nursingcenter.entiry.Customer;
import cn.edu.scau.nursingcenter.service.impl.Check_inServiceImpl;
import cn.edu.scau.nursingcenter.service.impl.CustomerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
/**
 * 客户入住信息控制器
 * @author feng
 *
 */
@Api(tags = "客户入住信息控制器")
@RequestMapping(value = "check_in")
@RestController
public class Check_inController {
	@Autowired
	private Check_inServiceImpl check_inServiceImpl;
	@Autowired
	private CustomerServiceImpl customerServiceImpl;
	
	/**
	  * 显示所有入住信息
	 * @return
	 */
	@ApiOperation(value = "显示所有入住记录")
	@GetMapping(value = "/list")
	@ResponseBody
	public ResultBean list() {
		List<Check_in> check_ins = check_inServiceImpl.selectAll();
		ResultBean resultBean = new ResultBean(200,true,"显示所有入住信息成功",check_ins);
		return resultBean;
	}
	
	/**
	  * 新增入住信息
	 * @param check_in对象
	 * @return
	 */
//	@PostMapping(value = "/insert")
//	@ResponseBody
//	public ResultBean insert(@Validated @RequestBody Check_in check_in) {
//		ResultBean resultBean = null;
//		Customer customer = Customer.builder()
//				.c_name(check_in.getC_name())
//				.c_gender(check_in.getC_gender())
//				.c_age(check_in.getC_age())
//				.c_telephone(check_in.getC_telephone())
//				.contacts(check_in.getContacts()).build();
//		Customer targCustomer = customerServiceImpl.insert(customer);
//		Integer new_id = targCustomer.getC_id();
//		check_in.setC_id(new_id);
//		Check_in targerCheck_in = check_inServiceImpl.insert(check_in);
//		if(targerCheck_in != null) {
//			resultBean = new ResultBean(200,true,"入住成功",targerCheck_in);
//		}else {
//			resultBean = new ResultBean(5007,false,"入住失败",null);
//		}
//		return resultBean;
//		
//	}
	
	/**
	  * 根据名字查询
	 * @param name
	 * @return
	 */
	@ApiOperation(value = "根据客户名字查询入住记录")
	@ApiImplicitParam(name = "name",value = "客户姓名",required = true, dataType = "String")
	@GetMapping(value = "/find")
	@ResponseBody
	public ResultBean findByName(@RequestParam(defaultValue = "")String name) {
		ResultBean resultBean = null;
		List<Check_in> check_ins = check_inServiceImpl.findByName(name);
		if(check_ins.size()== 0) {
			resultBean = new ResultBean(5009,false,"查询失败",null);
		}else {
			resultBean = new ResultBean(200,true,"查询成功",check_ins);
		}
		return resultBean;
	}
	/**
	 * 修改
	 * @param check_in
	 * @return
	 */
//	@ResponseBody
//	@PostMapping(value = "/update")
//	public ResultBean update(@Validated @RequestBody Check_in check_in) {
//		ResultBean resultBean = null;
//		Check_in targetCheck_in = check_inServiceImpl.update(check_in);
//		if(targetCheck_in != null) {
//			resultBean = new ResultBean(200, true,"修改成功",targetCheck_in);
//		}else {
//			resultBean = new ResultBean(5000,false,"修改失败",null);
//		}
//		return resultBean;
//	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据编号删除")
	@ApiImplicitParam(paramType = "path",name = "id",value = "编号",required = true,dataType = "int")
	@ResponseBody
	@DeleteMapping(value = "/delete/{id}")
	public ResultBean delete(@PathVariable Integer id) {
		ResultBean resultBean = null;
		if(id != null) {
			check_inServiceImpl.deleteById(id);
			resultBean = new ResultBean(200,true,"删除成功",null);			
		}else {
			resultBean = new ResultBean(5000,false,"删除失败",null);
		}
		return resultBean;
	}
	/**
	 * 新增入住或修改入住信息
	 * @param check_in
	 * @return
	 */
	@ApiOperation(value = "新增或者修改入住记录")
	@ResponseBody
	@PostMapping(value = "/save")
	public ResultBean save(@Validated @RequestBody Check_in check_in) {
		ResultBean resultBean = null;
		//判断是新增还是修改
		if(check_in.getCheck_in_id() == null) {
			//新增 返回 code = 200
			Customer customer = Customer.builder()
					.c_name(check_in.getC_name())
					.c_gender(check_in.getC_gender())
					.c_age(check_in.getC_age())
					.c_telephone(check_in.getC_telephone())
					.p_id(check_in.getP_id())
					.contacts(check_in.getContacts()).build();
			Customer targCustomer = customerServiceImpl.insert(customer);
			Integer new_id = targCustomer.getC_id();
			check_in.setC_id(new_id);
			Check_in targerCheck_in = check_inServiceImpl.insert(check_in);
			if(targerCheck_in != null) {
				resultBean = new ResultBean(200,true,"入住成功",targerCheck_in);
			}else {
				resultBean = new ResultBean(5007,false,"入住失败",null);
			}
		}else {
			//修改 返回 code =201
			Check_in targetCheck_in = check_inServiceImpl.update(check_in);
			if(targetCheck_in != null) {
				resultBean = new ResultBean(201, true,"修改成功",targetCheck_in);
			}else {
				resultBean = new ResultBean(5000,false,"修改失败",null);
			}
		}
		
		return resultBean;
	}
	

}
